package com.zueb.stumanage.logic

import android.util.Log
import androidx.lifecycle.liveData
import com.zueb.stumanage.logic.model.LoginData
import com.zueb.stumanage.logic.model.UserLogin
import com.zueb.stumanage.logic.model.UserLoginResponse
import com.zueb.stumanage.logic.network.ServiceCreator
import com.zueb.stumanage.logic.network.UserLoginNetwork
import com.zueb.stumanage.logic.network.UserService
import kotlinx.coroutines.Dispatchers
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

object Repository {


    fun userLogin(user:LoginData) = liveData(Dispatchers.IO) {
        val result= try {
            Log.d("hello","进入Repository")
            val userLoginResponse = UserLoginNetwork.userLoginNetwork(user)
            if (userLoginResponse.code == "200") {
                val userLogin = userLoginResponse.data
                Log.d("获取stuList",userLogin.toString())
                Result.success(userLogin)
               /* Log.d("获取result",Result.toString())*/
            } else {
                Result.failure(RuntimeException("response body is ${userLoginResponse.code}"))
            }
        } catch (e: Exception) {
           Result.failure<UserLogin>(e)
        }
        emit(result)
    }

    fun go(user:LoginData){
        val userService= ServiceCreator.create(UserService::class.java)
        userService.userLogin(user).enqueue(object : Callback<UserLoginResponse> {
            override fun onResponse(
                call: Call<UserLoginResponse>,
                response: Response<UserLoginResponse>
            ) {
                val loginResponse=response.body()
                if (loginResponse!=null)
                {
                    Log.d("login",loginResponse.toString())

                }
            }
            override fun onFailure(call: Call<UserLoginResponse>, t: Throwable) {
               Log.d("login","失败")
            }
        })








    }


}